const App = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    dataType: 'receive',

    isLoading: true,
    page: 1,
    no_more: false,
    rand:1,

    room_id:0,
    sex:1,
    shoppingMall:[],
    shoppingMallName:'',
    shoppingMallId:'',
    // 下拉菜单
    first: '选择商城',
    second: '筛选',
    thirds: '房型',
    fours: '筛选',
    _num: 0,
    _res: 0,
    dist:[],
    shopping_mall_city:'',
    shopping_mall_district:'',
    // 筛选
    sex_res: [{ id: 1, name: '男' }, { id: 2, name: '女' }],
    age_res: [{ id: 1, name: '20岁以下' }, { id: 2, name: '20岁-25岁' }, { id: 3, name: '26岁-30岁' }, { id: 4, name: '31岁-35岁' }, { id: 5, name: '36岁以上' }],
    status_res: [{ id: 1, name: '待撩' }, { id: 2, name: '单身' }, { id: 3,name: '有主' }],
    zhuangxiu: [{ name: '单拍' }, { name: '亲子套餐' }, { name: '活动套餐' }, { name: '女王套餐' }],
    leibei: [{ name: '单拍' }, { name: '亲子套餐' }, { name: '活动套餐' }, { name: '女王套餐' }],
    tese: [{ name: '单拍' }, { name: '亲子套餐' }, { name: '活动套餐' }, { name: '女王套餐' }],
    paixu: [{ name: '单拍' }, { name: '亲子套餐' }, { name: '活动套餐' }, { name: '女王套餐' }],
    one: 0,
    two: 0,
    third: 0,
    four: 0,
    five: 0,
    six: 0,
    seven: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let shopping_mall_city = wx.getStorageSync('shopping_mall_city');//城市名称
    let shopping_mall_district = wx.getStorageSync('shopping_mall_district');//区名称
    let user_address_id = wx.getStorageSync('user_address_id');//商城id
    let user_address_cont = wx.getStorageSync('user_address_cont');//商城名称
    //缓存中有
    if (shopping_mall_city) {
      this.setData({
        shopping_mall_city: shopping_mall_city
      })
    }
    if (shopping_mall_district) {
      this.setData({
        shopping_mall_district: shopping_mall_district
      })
    }
    if (user_address_id){
      this.setData({
        shoppingMallId: user_address_id
      })
    }
    if (user_address_cont){
      this.setData({
        shoppingMallName: user_address_cont
      })
    }
    // this.setData({
    //   isLoading: true,
    //   disdataTypet: 1,
    //   page: 1,
    //   no_more: false,
    //   rand: 1
    // });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let team = this.data.list;
    //获取 区信息
    this.getDistrict();
    // 获取 商城地址
    this.getShoppingMall();
    // 获取我的团队列表
    if(!team){
      this.getTeamList();
    }
  },

  onReady: function () {
    //页面渲染完成 第三运行
    //console.log('onReady');
  },
  //获取区信息
  getDistrict:function(){
    let _this = this;
    let user_address_id =  this.data.shoppingMallId
    let shopping_mall_city = this.data.shopping_mall_city;
    let shopping_mall_district = this.data.shopping_mall_district;
    
    App._get('Shopping_mall/getDistrict', {
      shopping_mall_id: user_address_id, 
      shopping_mall_city: shopping_mall_city,
      shopping_mall_district: shopping_mall_district
    }, function (result) {
      _this.setData({
        dist: result.data.list
      });
    });
  },
  //获取商城地址
  getShoppingMall:function(){
    let _this = this;
    //let user_address_id = wx.getStorageSync('user_address_id');
    let user_address_id = this.data.shoppingMallId;
    App._get('Shopping_mall/getShoppingMall', {
      shopping_mall_id: user_address_id
    }, function (result) {
      _this.setData({
        shoppingMall: result.data.list
      });
    });
  },

  /**
   * 获取我的团队列表
   */
  getTeamList: function(isNextPage, page) {
    let _this = this;
    let shoppingMallId = _this.data.shoppingMallId;
    //console.log(_this.data.rand);
    App._get('coupon/lists', {
      page: page || 1
    }, function(result) {
      //console.log(result);
      // 创建页面数据
      let tang = _this.createData(result.data, isNextPage);
      _this.setData(tang);
    });
  },

  /**
   * 创建页面数据
   */
  createData: function(data, isNextPage) {
    data['isLoading'] = false;
    // 列表数据
    let dataList = this.data.list;
    if (isNextPage == true && (typeof dataList !== 'undefined')) {
      data.list.data = dataList.data.concat(data.list.data);
    }
    
    // 设置swiper的高度
    this.setSwiperHeight(data.setting.level > 1);
    return data;
  },

  /**
   * 下拉到底加载数据
   */
  triggerDownLoad: function() {
    // 已经是最后一页
    if (this.data.page >= this.data.list.last_page) {
      this.setData({
        no_more: true
      });
      return false;
    }
    this.getTeamList(true, ++this.data.page);
  },
  //随机刷新
  rand:function(){
    this.setData({
      isLoading: true,
      disdataTypet: 1,
      page: 1,
      no_more: false,
      rand: 2
    });
    this.getTeamList();
  },
  //刷新页面
  newPage:function(){
    this.setData({
      isLoading: true,
      disdataTypet: 1,
      page: 1,
      no_more: false,
      rand: 1,
      currentTab:3
    });
    this.getTeamList();

  },
  //返回正常排序
  gotoSort:function(){
    this.setData({
      isLoading: true,
      disdataTypet: 1,
      page: 1,
      no_more: false,
      rand: 1
    });
    this.getTeamList();
  },
  /**
   * 设置swiper的高度
   */
  setSwiperHeight: function(isTap) {
    // 获取系统信息(拿到屏幕宽度)
    let systemInfo = wx.getSystemInfoSync(),
      rpx = systemInfo.windowWidth / 750, // 计算rpx
      tapHeight = isTap ? Math.floor(rpx * 82) : 0, // tap高度
      peopleHeight = Math.floor(rpx * 65), // people高度
      tang = Math.floor(rpx * 170), 
      swiperHeight = systemInfo.windowHeight - tapHeight - peopleHeight-tang; // swiper高度
    
    this.setData({
      swiperHeight
    });
  },
  /**
   * 表单提交
   */
  // saveData: function (e) {
  //   let _this = this;
  //   let shoppingMallId = _this.data.shoppingMallId;
  //   App._get('user.dealer.team/lists', {
  //     page: 1,
  //     shoppingMallId: shoppingMallId
  //   }, function (result) {
  //     // 创建页面数据
  //     _this.setData(_this.createData(result.data));
  //   });
  // },

  /**
   * 修改地区
   */
  // bindPickerChange: function (e) {
  //   let index = e.detail.value;
  //   this.setData({
  //     shoppingMallName: this.data.shoppingMall[index]['shopping_mall_cont'],
  //     shoppingMallId: this.data.shoppingMall[index]['shopping_mall_id']
  //   })
  // },


  //下拉js
  isShow: true,
  currentTab: 0,

  // 下拉切换
  hideNav: function () {
    this.setData({
      displays: "none"
    })
  },
  // 区域
  tabNav: function (e) {
    let dis = this.data.displays;
    if (dis == 'block' && this.data.currentTab === e.target.dataset.current){
      this.setData({
        displays: "none"
      })
    }else{
      this.setData({
        displays: "block"
      })
    }
    
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {

      var showMode = e.target.dataset.current == 0;

      this.setData({
        currentTab: e.target.dataset.current,
        isShow: showMode
      })
      
    }
  },
  // 下拉切换中的切换
  // 选择 区
  selected: function (e) {
    let _this = this;
    let dist_id = e.currentTarget.dataset.id;
    //let user_address_id = wx.getStorageSync('user_address_id');//商城id
    let user_address_id = this.data.shoppingMallId
    App._get('Shopping_mall/clickDist', {
      dist_id: dist_id,
      shopping_mall_id: user_address_id
    }, function (result) {
      _this.setData({
        dist: result.data.dist_res
      });
      _this.setData({
        shoppingMall: result.data.mall
      });
    });


  },
  
  // 商城选择
  clickShoppingMall: function (e) {
    let shoppingMall = this.data.shoppingMall;
    for (let i = 0; i < shoppingMall.length; i++) {
      shoppingMall[i]['selected'] = 'add_city';
      if (e.currentTarget.dataset.id == shoppingMall[i]['id']){
        shoppingMall[i]['selected'] = 'add_adress';
      }
    };
    this.setData({
      shoppingMall: shoppingMall,
      shoppingMallId: e.currentTarget.dataset.shopping_mall_id,
      shoppingMallName: e.currentTarget.dataset.name
    });
    this.getTeamList();//显示 社交对象
    this.setData({
      displays: "none"
    })
  },
  
  clickMum: function (e) {
    this.setData({
      _mum: e.target.dataset.num
    })
    this.setData({
      displays: "none"
    })
    var text = this.data.name
  },
 
  clickCum: function (e) {
    this.setData({
      _cum: e.target.dataset.num
    })
    this.setData({
      displays: "none"
    })
    var text = this.data.name
    console.log(text)
  },
  
  // 售价
  clickNum: function (e) {
    //console.log(e.target.dataset.num)
    this.setData({
      _num: e.target.dataset.num
    })
    this.setData({
      second: e.target.dataset.name
    })
    this.setData({
      displays: "none"
    })
    var text = this.data.name
    //console.log(text)
  },
  
  // 房型
  clickHouse: function (e) {
    this.setData({
      _res: e.target.dataset.num
    })
    this.setData({
      thirds: e.target.dataset.name
    })
    this.setData({
      displays: "none"
    })
  },
  

  // 筛选
  choseTxtColor: function (e) {
    var id = e.currentTarget.dataset.id;  //获取自定义的ID值  
    //console.log(e.currentTarget.dataset.id)
    this.setData({
      one: id
    })
  },
  chaoxiang: function (e) {
    var id = e.currentTarget.dataset.id;  //获取自定义的ID值  
    this.setData({
      two: id
    })
  },
  louceng: function (e) {
    var id = e.currentTarget.dataset.id;  //获取自定义的ID值  
    this.setData({
      third: id
    })
  },
  zhuangxiu: function (e) {
    var id = e.currentTarget.dataset.id;  //获取自定义的ID值  
    this.setData({
      four: id
    })
  },
  leibei: function (e) {
    var id = e.currentTarget.dataset.id;  //获取自定义的ID值  
    this.setData({
      five: id
    })
  },
  tese: function (e) {
    var id = e.currentTarget.dataset.id;  //获取自定义的ID值  
    this.setData({
      six: id
    })
  },
  paixu: function (e) {
    var id = e.currentTarget.dataset.id;  //获取自定义的ID值  
    this.setData({
      seven: id
    })
  },

  /**
   * 切换标签
   */
  bindHeaderTap: function (e) {
    let urls = {
      receive: '/pages/coupon/index?type=receive',
      assign: '/pages/order/index?type=assign',
      init: '/pages/doing/index?type=init',
    };
    // 转跳指定的页面
    wx.navigateTo({
      url: urls[e.currentTarget.dataset.type]
    })
  },

})